import uuid
import itertools

from django import http
from django import forms
from django.conf import settings
from django.core import serializers
from django.template import loader

from EE.logic import game as game_logic
from EE.logic import game_state as game_state_logic
from EE.logic import user_profile as user_profile_logic

from EE.models import Game
from EE.models import Windmill
from EE.views import handler


class GameForm(forms.Form):
  name = forms.CharField(max_length=100)
  windmills = forms.ModelMultipleChoiceField(Windmill.objects.all())
  #users = forms.ModelMultipleChoiceField(self.user.objects.filter(Group='Groep'), required=False)

class Home(handler.Handler):
  """Handler to handle the game views"""
  
  def authenticated(self):
    return self.profile_type == 'Leraar'

  def post(self):
    return http.HttpResponseRedirect('../home')
    
  def get(self):
    context = self.getContext()

    prof = user_profile_logic.getProfile(self.user)
    games = list(prof.gameControl.all())
	
    result = []
    for g in games:
	  g.turn = str(g.turn)
	  if g.storm:
	    g.storm = 'Ja'
	  else:
	    g.storm = 'Nee'
	  g.storm = str(g.storm)
	  result.append((g, game_logic.getPlayers(g), game_logic.getMills(g)))
	
    context['games'] = result

    content = loader.render_to_string('game/home.html', dictionary=context)
    return http.HttpResponse(content)


class NewGame(handler.Handler):
  """Handler for the creation of a new game.
  """

  def authenticated(self):
    return self.profile_type == 'Leraar'

  def getGetForm(self):
    return GameForm()

  def getPostForm(self):
    return GameForm(self.POST)

  def post(self):
    name = self.cleaned_data['name']
    #users = self.cleaned_data.get('users', [])
    mills = self.cleaned_data.get('windmills', [])

    game = game_logic.handle_add_game(self.user.id, name, mills)
    #game_state = game_state_logic.createNew(self.user)
    #game_logic.handle_select_users(game, users)

    return http.HttpResponseRedirect('./home')

  def get(self):
    context = self.getContext()
    content = loader.render_to_string('game/new.html', dictionary=context)
    return http.HttpResponse(content)
	
class EditGame(handler.Handler):
  
  def authenticated(self):
    game = self.kwargs['id_str']
    return (self.profile_type == 'Leraar') and (game in [i.id for i in self.profile.gameControl.all()])
	
  def getGetForm(self):
    game_id = self.kwargs['id_str']
    game = Game.objects.filter(id=game_id).get()
    initial = {
	    'name': game.name,
	    'windmills': [i.id for i in Windmill.objects.all()],
	}
    return GameForm(initial=initial)

  def getPostForm(self):
    return GameForm(self.POST)

  def post(self):
    name = self.cleaned_data['name']
    #users = self.cleaned_data.get('users', [])
    mills = self.cleaned_data.get('windmills', [])
    game = game_logic.handle_edit_game(self.kwargs['id_str'], name, mills)
    #game_logic.handle_select_users(game, users)

    return http.HttpResponseRedirect(self.BASE_PATH + 'game/home')

  def get(self):
    context = self.getContext()
    context['game'] = Game.objects.filter(id=self.kwargs['id_str']).get()
    content = loader.render_to_string('game/edit.html', dictionary=context)
    return http.HttpResponse(content)